/**
 * @title DateFormat.java
 * @description 时间格式工具类
 * @package com.sunyard.mp.util
 * @project CTS
 * @copyright ©2015 Sunyard Technology
 * @author Nan Ou
 * @email ou.nan@sunyard.com
 * @date 2015年10月26日 上午10:52:56
 * @version V1.0
 */
package com.sunyard.manage.util;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author Nan Ou
 * @className DateFormat
 * @description 时间格式工具类
 * @copyright ©2015 Sunyard Technology
 * @email ou.nan@sunyard.com
 * @date 2015年10月28日 上午10:04:19
 */
public class DateFormatUtil {

    /**
     * Description
     */
    public final static String YYYYMMDD = "yyyyMMdd";
    /**
     * Description
     */
    public final static String YYMMDDHHMM = "yyMMddHHmm";
    /**
     * Description
     */
    public final static String YY_MM_DD = "yyyy-MM-dd";
    /**
     * Description
     */
    public final static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    /**
     * Description
     */
    public final static String YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前时间
     *
     * @return yyyy-MM-dd HH:mm:ss格式当前时间
     */
    public static String getDefaultTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD_HHMMSS);
        return sdf.format(new Date());
    }

    /**
     *如果当前时间减去7天 大于创建时间 返回true否则返回false
     * @param date
     * @return
     */
    public static Boolean  dateCompute(String date){
        Date date1 = new Date();
        Date newTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD_HHMMSS);
        try {
            newTime = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long nowDate = date1.getTime() - 7 * 24 * 60 * 60 * 1000;
        long createTime = newTime.getTime();
        if (nowDate >= createTime){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param format
     * @return
     */
    public static String dateTimeFormat(Date date, String format) {
        if (date == null || StringUtils.isEmpty(format)) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * @param date
     * @return
     * @title getYear
     * @description 获取年份
     * @author Nan Ou
     * @email ou.nan@sunyard.com
     */
    public static String getYear(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(date);
    }

    /**
     * @param date
     * @return
     * @title getMonth
     * @description 获取月份
     * @author Nan Ou
     * @email ou.nan@sunyard.com
     */
    public static String getMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        return sdf.format(date);
    }

    /**
     * @param date
     * @return
     * @title getDay
     * @description 获取日份
     * @author Nan Ou
     * @email ou.nan@sunyard.com
     */
    public static String getDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        return sdf.format(date);
    }

    /**
     * @Title getDate @Description 根据指定格式以及时间构造Date @param @param
     * format @param @param date @param @return @param @throws
     * ParseException @return Date @throws
     */
    public static Date getDate(String format, String date) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(date);
    }

    /**
     * Description
     */
    public static Date getWebsiteDatetime(String webUrl) {
        try {
            URL url = new URL(webUrl);//取得资源对象
            URLConnection uc = url.openConnection();//生成连接对象
            uc.connect();//发出连接
            long ld = uc.getDate();//读取网站日期时间
            Date date = new Date(ld);//转换为标准时间对象
            return date;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
